/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * countryCreator.java
 *
 * Created on 25/04/2011, 12:21:54
 */
package creator;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ListModel;
import utn.tsb.tej.world_logic.CardType;
import utn.tsb.tej.world_logic.Color;
import utn.tsb.tej.world_logic.Country;
import csm.uttils.Archive;
import csm.uttils.generics.Roster;

/**
 *
 * @author Administrator
 */
public class countryCreator extends javax.swing.JFrame {

    /** Creates new form countryCreator */
    public countryCreator() {
        //initComponents();
        countries = new Roster<Country>();
        lstCountry.setModel(countriesModel);
        lstLandBordering.setModel(landModel);
        lstSeaBordering.setModel(seaModel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        lblName = new javax.swing.JLabel();
        lblCodeRgb = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lblX = new javax.swing.JLabel();
        lblY = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        txtCodeRGB = new javax.swing.JTextField();
        txtX = new javax.swing.JTextField();
        txtY = new javax.swing.JTextField();
        cbxJoker = new javax.swing.JCheckBox();
        cbxAir = new javax.swing.JCheckBox();
        cbxWater = new javax.swing.JCheckBox();
        cbxEarth = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        btnEdit = new javax.swing.JButton();
        btnAddSea = new javax.swing.JButton();
        btnAddLand = new javax.swing.JButton();
        btnSubLand = new javax.swing.JButton();
        btnSubSea = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        btnLoad = new javax.swing.JButton();
        btnAddCountry = new javax.swing.JButton();
        scpCountries = new javax.swing.JScrollPane();
        jScrollPane4 = new javax.swing.JScrollPane();
        lstCountry = new javax.swing.JList();
        jScrollPane1 = new javax.swing.JScrollPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstLandBordering = new javax.swing.JList();
        jScrollPane5 = new javax.swing.JScrollPane();
        jScrollPane3 = new javax.swing.JScrollPane();
        lstSeaBordering = new javax.swing.JList();
        btnNewCountry = new javax.swing.JButton();

        jLabel2.setText("jLabel2");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblName.setText("Nombre");
        getContentPane().add(lblName, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 14, -1, -1));

        lblCodeRgb.setText("CodeRGB");
        getContentPane().add(lblCodeRgb, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 10, -1, -1));

        jLabel4.setText("Types");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, -1, -1));

        lblX.setText("X");
        getContentPane().add(lblX, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 10, 10, -1));

        lblY.setText("Y");
        getContentPane().add(lblY, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 10, -1, -1));
        getContentPane().add(txtName, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 10, 90, -1));
        getContentPane().add(txtCodeRGB, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 10, 90, -1));
        getContentPane().add(txtX, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 10, 29, -1));
        getContentPane().add(txtY, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 10, 34, -1));

        cbxJoker.setText("JOKER");
        getContentPane().add(cbxJoker, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 70, -1, -1));

        cbxAir.setText("AIR");
        getContentPane().add(cbxAir, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 50, -1, -1));

        cbxWater.setText("WATER");
        getContentPane().add(cbxWater, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 70, -1, -1));

        cbxEarth.setText("EARTH");
        getContentPane().add(cbxEarth, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 50, -1, -1));

        jLabel1.setText("Country");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(29, 114, -1, -1));

        jLabel3.setText("LandBordering");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(221, 113, -1, -1));

        jLabel5.setText("SeaBordering");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 110, 100, -1));

        btnEdit.setText("Editar");
        btnEdit.setEnabled(false);
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        getContentPane().add(btnEdit, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 295, -1, -1));

        btnAddSea.setText("Sea>>");
        btnAddSea.setEnabled(false);
        btnAddSea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddSeaActionPerformed(evt);
            }
        });
        getContentPane().add(btnAddSea, new org.netbeans.lib.awtextra.AbsoluteConstraints(128, 185, 73, -1));

        btnAddLand.setText("Land>>");
        btnAddLand.setEnabled(false);
        getContentPane().add(btnAddLand, new org.netbeans.lib.awtextra.AbsoluteConstraints(128, 214, -1, -1));

        btnSubLand.setText("<<");
        btnSubLand.setEnabled(false);
        getContentPane().add(btnSubLand, new org.netbeans.lib.awtextra.AbsoluteConstraints(207, 295, -1, -1));

        btnSubSea.setText("<<");
        btnSubSea.setEnabled(false);
        btnSubSea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSubSeaActionPerformed(evt);
            }
        });
        getContentPane().add(btnSubSea, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 300, 50, -1));

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        getContentPane().add(btnSave, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 366, -1, -1));

        btnLoad.setText("Load");
        btnLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadActionPerformed(evt);
            }
        });
        getContentPane().add(btnLoad, new org.netbeans.lib.awtextra.AbsoluteConstraints(79, 366, -1, -1));

        btnAddCountry.setText("Add");
        btnAddCountry.setEnabled(false);
        btnAddCountry.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddCountryActionPerformed(evt);
            }
        });
        getContentPane().add(btnAddCountry, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 50, 60, 53));

        lstCountry.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstCountryValueChanged(evt);
            }
        });
        jScrollPane4.setViewportView(lstCountry);

        scpCountries.setViewportView(jScrollPane4);

        getContentPane().add(scpCountries, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 130, 100, 155));

        jScrollPane2.setViewportView(lstLandBordering);

        jScrollPane1.setViewportView(jScrollPane2);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 130, 100, 160));

        jScrollPane3.setViewportView(lstSeaBordering);

        jScrollPane5.setViewportView(jScrollPane3);

        getContentPane().add(jScrollPane5, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 130, 100, 160));

        btnNewCountry.setText("new");
        btnNewCountry.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewCountryActionPerformed(evt);
            }
        });
        getContentPane().add(btnNewCountry, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 50, 60, 50));

        pack();
    }// </editor-fold>//GEN-END:initComponents
*/
    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        Country country = getCountryByName(lstCountry.getSelectedValue().toString());
        loadFields(country);
        btnAddCountry.setEnabled(true);
        btnAddLand.setEnabled(true);
        btnAddSea.setEnabled(true);
    }//GEN-LAST:event_btnEditActionPerformed

    private void btnAddCountryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddCountryActionPerformed
        String name = txtName.getText();
        String codeRGB = txtCodeRGB.getText();
        int x = 0;
        int y = 0;
        if (txtX.getText().compareTo("") != 0) {
            x = Integer.parseInt(txtX.getText());
        }
        if (txtY.getText().compareTo("") != 0) {
            y = Integer.parseInt(txtY.getText());
        }
        Country country = new Country();
        country.setColor(new Color(codeRGB));
        country.setName(name);
        country.setxPosition(x);
        country.setyPosition(y);
        country.setSeaBorderingCountries(new Roster<Country>());
        country.setLandBorderingCountries(new Roster<Country>());
        if (cbxEarth.isSelected()) {
            country.addType(CardType.EARTH);
        }
        if (cbxWater.isSelected()) {
            country.addType(CardType.WATER);
        }
        if (cbxAir.isSelected()) {
            country.addType(CardType.AIR);
        }
        if (cbxJoker.isSelected()) {
            country.addType(CardType.JOKER);
        }
        if (getCountryByName(txtName.getText()) == null) {
            insertCountry(country);
        } else {
            updateCountry(country);
        }
        btnAddCountry.setEnabled(false);
        btnEdit.setEnabled(false);
        btnAddSea.setEnabled(false);
        btnAddLand.setEnabled(false);
    }//GEN-LAST:event_btnAddCountryActionPerformed

    private void lstCountryValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstCountryValueChanged
        int index = lstCountry.getSelectedIndex();
        lstCountry.setSelectedIndex(index);
        btnEdit.setEnabled(true);

    }//GEN-LAST:event_lstCountryValueChanged

    private void btnAddSeaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddSeaActionPerformed
        Country countryToAdd = getCountryByName(lstCountry.getSelectedValue().toString());
        getCountryByName(lstCountry.getSelectedValue().toString()).getSeaBorderingCountries().addLast(countryToAdd);
        fillList(lstSeaBordering, getCountryByName(lstCountry.getSelectedValue().toString()).getSeaBorderingCountries());
    }//GEN-LAST:event_btnAddSeaActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        Archive archive = new Archive("tjFiles\\countries.ctj");
        archive.writeRoster(countries);
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadActionPerformed
        Archive archive = new Archive("tjFiles\\countries.ctj");
        countries = (Roster<Country>) archive.readRoster();
        fillList(lstCountry, countries);
    }//GEN-LAST:event_btnLoadActionPerformed

    private void btnNewCountryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewCountryActionPerformed
        clearFields();
        btnAddCountry.setEnabled(true);
    }//GEN-LAST:event_btnNewCountryActionPerformed

    private void btnSubSeaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubSeaActionPerformed

    }//GEN-LAST:event_btnSubSeaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new countryCreator().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddCountry;
    private javax.swing.JButton btnAddLand;
    private javax.swing.JButton btnAddSea;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnLoad;
    private javax.swing.JButton btnNewCountry;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnSubLand;
    private javax.swing.JButton btnSubSea;
    private javax.swing.JCheckBox cbxAir;
    private javax.swing.JCheckBox cbxEarth;
    private javax.swing.JCheckBox cbxJoker;
    private javax.swing.JCheckBox cbxWater;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JLabel lblCodeRgb;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblX;
    private javax.swing.JLabel lblY;
    private javax.swing.JList lstCountry;
    private javax.swing.JList lstLandBordering;
    private javax.swing.JList lstSeaBordering;
    private javax.swing.JScrollPane scpCountries;
    private javax.swing.JTextField txtCodeRGB;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtX;
    private javax.swing.JTextField txtY;
    // End of variables declaration//GEN-END:variables
    private Roster<Country> countries;
//    private Roster<Country> rstSeaCountries = new Roster<Country>();
//    private Roster<Country> rstLandCountries = new Roster<Country>();
    DefaultListModel countriesModel = new DefaultListModel();
    DefaultListModel seaModel = new DefaultListModel();
    DefaultListModel landModel = new DefaultListModel();

    private void fillList(JList lstCountry, Roster<Country> countries) {
        ((DefaultListModel) lstCountry.getModel()).clear();
        for (int i = 0; i < countries.count(); i++) {

            ((DefaultListModel) lstCountry.getModel()).addElement(countries.getElementInPosition(i).getName());
        }
    }

    private Country getCountryByName(String name) {
        Country country = null;
        for (int i = 0; i < countries.count(); i++) {
            if (countries.getElementInPosition(i).getName().compareToIgnoreCase(name) == 0) {
                country = countries.getElementInPosition(i);
                break;
            }
        }
        return country;
    }

    private void loadFields(Country country) {
        clearFields();
        txtName.setText(country.getName());
        txtCodeRGB.setText(country.getColor().getCodeRGB());
        txtX.setText(country.getxPosition() + "");
        txtY.setText(country.getyPosition() + "");
        int[] types = country.getType().getTypeValues();
        for (int i = 0; i < types.length; i++) {
            if (types[i] == CardType.EARTH) {
                cbxEarth.setSelected(true);
            }
            if (types[i] == CardType.WATER) {
                cbxWater.setSelected(true);
            }
            if (types[i] == CardType.AIR) {
                cbxAir.setSelected(true);
            }
            if (types[i] == CardType.JOKER) {
                cbxJoker.setSelected(true);
            }
        }
//        rstSeaCountries = country.getSeaBorderingCountries();
//        rstLandCountries = country.getLandBorderingCountries();
        fillList(lstSeaBordering, country.getSeaBorderingCountries());
        fillList(lstLandBordering, country.getLandBorderingCountries());
    }

    private void insertCountry(Country country) {
        countries.addLast(country);
        fillList(lstCountry, countries);
    }

    private void updateCountry(Country country) {
        countries.remove(getCountryByName(country.getName()));
        countries.addLast(country);
        fillList(lstCountry, countries);
    }

    private void clearFields() {

        cbxAir.setSelected(false);
        cbxEarth.setSelected(false);
        cbxJoker.setSelected(false);
        cbxWater.setSelected(false);
        seaModel.clear();
        landModel.clear();
        txtCodeRGB.setText("");
        txtName.setText("");
        txtX.setText("");
        txtY.setText("");
//        rstSeaCountries = new Roster<Country>();
//        rstLandCountries = new Roster<Country>();
    }
}
